package com.lhys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhys.dto.AssetUserDto;
import com.lhys.mapper.AssetUserMapper;
import com.lhys.pojo.Asset;
import com.lhys.pojo.AssetUser;
import com.lhys.service.AssetUserService;
import com.lhys.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoqi
 * Date: 2021/5/14
 * Time: 下午 18:32
 * Description: 资产用户业务层接口实现类
 */
@Service
public class AssetUserServiceImpl implements AssetUserService {
    @Autowired
    private AssetUserMapper assetUserMapper;

    @Value("${login.password-error}")
    private String passwordErrorInfo;

    @Autowired
    private Md5Util md5Util;

    @Override
    @Transactional
    public Boolean saveAssetUser(AssetUser assetUser) {
        encodeUser(assetUser);
        if (assetUser.getId() == null) {
            if("".equals(assetUser.getPassword()) || null == assetUser.getPassword()){
                try {
                    assetUser.setPassword(md5Util.encodeByMd5(assetUser.getPhone().substring(5)));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return assetUserMapper.addAssetUser(assetUser) > 0;
        }
        return assetUserMapper.updateAssetUser(assetUser) > 0;
    }

    @Override
    @Transactional
    public Boolean delAssetUserById(Integer id) {
        return assetUserMapper.delAssetUserById(id) > 0;
    }

    @Override
    public AssetUser getAssetUserByLoginNameAndPassword(AssetUser assetUser) {
        encodeUser(assetUser);
        Map<String, Object> map = new HashMap<>();
        List<AssetUser> list = assetUserMapper.getAssetUserByLoginName(assetUser.getLoginName());
        if (list.size() == 0) {
            return null;
        }
        AssetUser loginAssetUser = assetUserMapper.getAssetUserByLoginNameAndPassword(assetUser);
        System.out.println(loginAssetUser);
        if (loginAssetUser == null) {
            AssetUser passwordError = new AssetUser();
            return passwordError.setPassword(passwordErrorInfo);
        }
        return loginAssetUser;
    }

    @Override
    public PageInfo<AssetUser> listAssetUserByPage(AssetUser assetUser, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<AssetUser> userList = assetUserMapper.listAssetUserByPage(assetUser);
        userList.forEach(item -> item.setPassword(null));
        PageInfo<AssetUser> pageInfo = new PageInfo<>(userList);
        return pageInfo;
    }

    @Override
    public Boolean updatePasswordById(AssetUserDto assetUserDto) {
        try {
            assetUserDto.setNewPassword(md5Util.encodeByMd5(assetUserDto.getNewPassword()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return assetUserMapper.updatePasswordById(assetUserDto) > 0;
    }

    private void encodeUser(AssetUser assetUser) {
        if (!"".equals(assetUser.getPassword()) && null != assetUser.getPassword()) {
            try {
                assetUser.setPassword(md5Util.encodeByMd5(assetUser.getPassword()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}